package com.example.myapplication

import android.os.Bundle
import android.view.Gravity
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    private var textCount = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        btnAddText.setOnClickListener {
            textCount++
            val dynamicText = TextView(this).apply {
                text = "动态文本 $textCount"
                textSize = 16f
                setPadding(0, 16, 0, 16)
                gravity = Gravity.CENTER_HORIZONTAL
                layoutParams = LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT
                )
            }
            llDynamicContainer.addView(dynamicText)
        }

        initMVC()
        class ProgramAdviserModel {
            fun getAdvice(query: String): String {
                return when {
                    query.contains("java", ignoreCase = true) ->
                        App.context.getString(R.string.advice_java)
                    query.contains("kotlin", ignoreCase = true) ->
                        App.context.getString(R.string.advice_kotlin)
                    query.contains("flutter", ignoreCase = true) ->
                        App.context.getString(R.string.advice_flutter)
                    else ->
                        App.context.getString(R.string.advice_unknown)
                }
            }
        }
        // 在MainActivity中添加
        private fun initMVC() {
            val model = ProgramAdviserModel()
            btnQuery.setOnClickListener {
                val query = etQuery.text.toString().trim()
                val advice = model.getAdvice(query)
                tvAdviceResult.text = advice
            }
        }
        object App : Application() {
            lateinit var context: Context
            override fun onCreate() {
                super.onCreate()
                context = applicationContext
            }
        }
    }

}